<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>欢迎使用田螺网络书城系统，用户登录</title>
    <style>
        body {
            font-family: Arial;
            background-color: #f4f4f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            width: 300px;
        }
        input {
            width: 100%;
            margin: 10px 0;
            padding: 10px;
            border-radius: 5px;
            border: 1px solid #ccc;
        }
        button {
            width: 100%;
            padding: 10px;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
        }
    </style>
</head>
<body>
<div class="login-container">

    <h2>田螺书店登录</h2>
    <form id="login-form">
        <input type="tel" name="phone" placeholder="手机号" required pattern="^\d{11}$">
        <input type="password" name="password" placeholder="密码" required>
        <button type="submit">登录</button>
    </form>
    <p id="message" style="color:red;"></p>
</div>

<script>
    document.getElementById('login-form').addEventListener('submit', async function(e) {
        e.preventDefault(); // 阻止默认表单提交

        const form = e.target;
        const phone = form.phone.value;
        const password = form.password.value;

        const res = await fetch('/user/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ phone, password })
        });

        const result = await res.json();
        console.log("result:"+result.data)
        if (result.code === '0000') {
            localStorage.setItem('token', result.data); // 保存 token
            window.location.href = '/home'; // 跳转页面
        } else {
            document.getElementById('message').innerText = result.message;
        }
    });
</script>

</body>
</html>
